package com.toggery.POS.controller;

import com.toggery.POS.entity.Clothing;
import com.toggery.POS.entity.Inventory;
import com.toggery.POS.service.ClothingService;
import com.toggery.POS.service.InventoryService;
import com.toggery.POS.tool.DateTool;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/clothing")
@Api(tags = "服装数据")
public class ClothingController {
    @Autowired
    ClothingService service;
    @Autowired
    InventoryService inventoryService;
    @GetMapping("/list")
    public Map findAll(){
        Map map = new HashMap() ;
        map.put("data",service.findAll());
        return map;
    }

    @GetMapping("/findbyname/{name}")
    public Map findByName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("all",service.findByName(name));
        return map;
    }

    @GetMapping("/findlikename/{name}")
    public Map findLikeName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("clothings",service.findLikeName(name));
        return map;
    }

    @PostMapping(value = "/delete/{id}")
    public Map deleteById(@PathVariable int id){
        Map map = new HashMap();
        if(service.deleteByid(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    public Map update(@RequestBody(required=false) Map map1){
        Clothing clothing = service.findById(Integer.parseInt(map1.get("id").toString()));
        clothing.setPrice(Double.parseDouble(map1.get("price").toString()));
        HashMap map = new HashMap();
        if(service.updateClothing(clothing)){
            map.put("result",true);
            map.put("result","更新成功");
        }else {
            map.put("result",false);
            map.put("result","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map save(@RequestBody(required=false) Map map1){
        Clothing clothing = new Clothing();
        clothing.setBrand(Integer.parseInt(map1.get("brand").toString()));
        clothing.setCategory(Integer.parseInt(map1.get("category").toString()));
        clothing.setPrice(Double.parseDouble(map1.get("price").toString()));
        clothing.setClothingname(map1.get("clothingname").toString());
        HashMap map = new HashMap();
        if(service.save(clothing)){
            Inventory inventory = new Inventory();
            inventory.setNumber(0);
            inventory.setClothingname(clothing.getClothingname());
            inventory.setType(2);
            inventory.setClothingid(service.findByName(clothing.getClothingname()).getId());
            inventory.setCreattime(DateTool.getNow());
            inventoryService.save(inventory);
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }

}
